package com.shop.mall.servlet;

import com.alibaba.fastjson.JSONObject;
import com.shop.mall.dao.impl.UserDaoImpl;
import com.shop.mall.entity.User;
import com.shop.mall.utils.BaseResultVO;
import com.shop.mall.utils.GetUUIDUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author admin
 * @version 1.0
 * @date 2019/5/13
 */
@WebServlet(urlPatterns = "/WebResgistServlet")
public class WebResgistServlet extends HttpServlet {
    private static final long serialVersionUID = -7539185293576339099L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("regpassword");
        String tel = request.getParameter("tel");
        String email = request.getParameter("regemail");
        String sex = request.getParameter("sex");
        String role = request.getParameter("role");
        String status = request.getParameter("status");

        PrintWriter out = response.getWriter();
        JSONObject object = new JSONObject(true);
        BaseResultVO resultVO = new BaseResultVO();
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            object.put("id", GetUUIDUtil.getId());
            object.put("username", username);
            object.put("password", password);
            object.put("tel", tel);
            object.put("email", email);
            object.put("sex", sex);
            object.put("role", role);
            object.put("status", status);

            resultVO.setResult(false);
            resultVO.setCode(-1);
            resultVO.setData(JSONObject.toJSONString(object));
            resultVO.setMessage("请填写用户完整信息后在尝试提交");
            out.println(resultVO);
            out.close();
            out.flush();
        }
        UserDaoImpl udao = new UserDaoImpl();
        User user = new User();
        user.setId(Long.valueOf(GetUUIDUtil.getId()));
        user.setName(username);
        user.setPassword(password);
        user.setTel(tel);
        user.setEmail(email);
        user.setSex(sex);
        user.setRole(role);
        user.setStatus(status);
        try {
            int flag = udao.regist(user);
            if (flag > 0) {
                resultVO.setResult(true);
                resultVO.setCode(0);
                resultVO.setData(JSONObject.toJSONString(user));
                System.out.println("操作成功," + resultVO);
                //请求重定向
                response.sendRedirect(request.getContextPath() + "/pages/webviews/login.html");
                return;
            }
            resultVO.setResult(false);
            resultVO.setCode(-1);
            resultVO.setData(JSONObject.toJSONString(user));
            System.out.println("操作失败," + resultVO);
            response.sendRedirect(request.getContextPath() + "/pages/webviews/register.html");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
